import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/login/Login'
import Register from '../views/login/Register'
import Home from '../views/home/Home'
import Cookies from "js-cookie";

Vue.use(VueRouter)

// 创建并暴露一个路由器
const router = new VueRouter({
    mode: 'history',    // 路由模式，该模式不会在地址中显示井号#
    routes: [
        {
            path: '/',          // 路径
            redirect: '/login'  // 重定向
        },
        {
            path: '/login',     // 路径
            name: 'login',
            component: Login    // 跳转到的组件
        },
        {
            path: '/register',     // 路径
            name: 'register',
            component: Register    // 跳转到的组件
        },
        {
            path: '/home',     // 路径
            name: 'home',
            component: Home    // 跳转到的组件
        },
        {
            path: '/phone',
            name: 'phone',
            component: () => import('../views/phone/Index.vue')
        },
        {
            path: '/gasoline',
            name: 'gasoline',
            component: () => import('../views/gasoline/Gasoline.vue')
        },
        {
            path: '/gasolineAll',
            name: 'gasolineAll',
            component: () => import('../views/gasoline/GasolineAll.vue')
        },
        {
            path: '/gasolineCount',
            name: 'gasolineCount',
            component: () => import('../views/gasolineCount/Index.vue')
        },
        {
            path: '/one',
            name: 'one',
            component: () => import('../views/gasolineCount/One.vue')
        },
        {
            path: '/two',
            name: 'two',
            component: () => import('../views/gasolineCount/Two.vue')
        },
        // {
        //     path: '/gasolineCount/three',
        //     name: 'three',
        //     component: () => import('../views/gasolineCount/Three.vue')
        // },
        // {
        //     path: '/gasolineCount/four',
        //     name: 'four',
        //     component: () => import('../views/gasolineCount/Four.vue')
        // },
        // {
        //     path: '/gasolineCount/five',
        //     name: 'five',
        //     component: () => import('../views/gasolineCount/Five.vue')
        // },
        // {
        //     path: '/gasolineCount/six',
        //     name: 'six',
        //     component: () => import('../views/gasolineCount/Six.vue')
        // },
        // {
        //     path: '/gasolineCount/seven',
        //     name: 'seven',
        //     component: () => import('../views/gasolineCount/Seven.vue')
        // },
        // {
        //     path: '/gasolineCount/eight',
        //     name: 'eight',
        //     component: () => import('../views/gasolineCount/Eight.vue')
        // },
    ]
})

//导航守卫

router.beforeEach((to, from, next) => {
    let isAuthenticated = Cookies.get('token')
    // 如果路由要跳转到除了登录和注册的界面的话就判断是否已经登录，如果没有登录就强制跳到登录界面
    if (to.path !== '/login' && to.path !== '/register' && !isAuthenticated) {
        next({path: '/login'})
    } else next()
})


export default router
